package com.module.aichat.db.table

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import com.common.app.helper.UserHelper
import com.common.app.utls.TimeUtils
import com.fungo.baselib.utils.UUIDUtil
import com.module.aichat.data.bean.MsgDirection
import com.module.aichat.data.protocol.messageContent.MessageContent
import com.module.aichat.data.protocol.Message

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/6/30
 * <p>
 * 描述：会话实体类
 * <p>
 * 修订历史：
 */
@Entity(tableName = "chat_message")
class DBMessage : Message() {

    //消息方向 1-代表发送  2-代表接收
    @ColumnInfo(name = "msgDirection")
    var msgDirection: Int = 0

    //消息是否需要入库
    @Ignore
    var needToInsertDB: Boolean = false


    companion object {
        fun obtainDBSendMessage(msgContent: MessageContent, targetId: String, sendStatus: Int): DBMessage {
            return DBMessage().apply {
                type = msgContent.type
                this.targetId = targetId
                sendTime = TimeUtils.getTimeDateLong()
                id = UUIDUtil.uuid
                senderId = UserHelper.getUid()
                msgDirection = MsgDirection.SEND
                this.sendStatus = sendStatus
                content = msgContent
                version = "1.0"
                needToInsertDB = true    //自己主动发送的消息也需要入库
            }
        }
        fun obtainDBReceiveMessage(msgContent: MessageContent, targetId: String, sendStatus: Int): DBMessage {
            return DBMessage().apply {
                type = msgContent.type
                this.targetId = targetId
                sendTime = TimeUtils.getTimeDateLong()
                id = UUIDUtil.uuid
                senderId = UserHelper.getUid()
                msgDirection = MsgDirection.RECEIVE
                this.sendStatus = sendStatus
                content = msgContent
                version = "1.0"
                needToInsertDB = true    //自己主动发送的消息也需要入库
            }
        }

        fun obtainDBMessage(msgContent: MessageContent, targetId: String, taskId: String, messageId: String, conversationId: String, sendStatus: Int): DBMessage {
            return DBMessage().apply {
                type = msgContent.type
                this.targetId = targetId
                this.taskId = taskId
                this.messageId = messageId
                this.conversationId = conversationId
                sendTime = TimeUtils.getTimeDateLong()
                id = UUIDUtil.uuid
                senderId = UserHelper.getUid()
                msgDirection = MsgDirection.SEND
                this.sendStatus = sendStatus
                content = msgContent
                version = "1.0"
                needToInsertDB = true    //自己主动发送的消息也需要入库
            }
        }
    }
}